

## TABLE 2

# load, combine data
library(rio)
library(tidyverse)

# estimate models
library(brglm2)

# cluster standard errors
library(lmtest)
library(sandwich)

# generate tables
library(stargazer)




# LOAD DATA
glm_df <- import("glm_data.csv") %>%
  filter(iso3c!="TTO" & iso3c!="GUY" & iso3c!="SUR") %>%
  filter(!is.na(approval) & !is.na(opp1vote) & !is.na(number_protests))


# ESTIMATE MODELS
brglm1 <- glm(doc ~ approval + opp1vote + number_protests +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              family = binomial(link="logit"), 
              method = "brglmFit",
              data = glm_df)

brglm1_r <- brglm1 %>%
  coeftest(vcovHC(brglm1, type = 'HC0', cluster =  ~ iso3c))


brglm2 <- glm(doc ~ previous_doc + duck + partyage + polity2 + left_exec + any_election + minister_is_technocrat_mainstream + 
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              family = binomial(link="logit"), 
              method = "brglmFit",
              data = glm_df)

brglm2_r <- brglm2 %>% 
  coeftest(vcovHC(brglm2, type = 'HC0', cluster =  ~ iso3c))



brglm3 <- glm(doc ~ resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
              as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
              family = binomial(link="logit"), 
              method = "brglmFit",
              data = glm_df)

brglm3_r <- brglm3 %>% 
  coeftest(vcovHC(brglm3, type = 'HC0', cluster =  ~ iso3c))



brglm4 <- glm(doc ~ approval + opp1vote + number_protests +
               previous_doc + duck + partyage + polity2 + left_exec + any_election + minister_is_technocrat_mainstream + 
               as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
               family = binomial(link="logit"), 
               method = "brglmFit",
               data = glm_df)

brglm4_r <- brglm4 %>% 
  coeftest(vcovHC(brglm4, type = 'HC0', cluster =  ~ iso3c))



brglm5 <- glm(doc ~ approval + opp1vote + number_protests + 
               resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
               as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
               family = binomial(link="logit"), 
               method = "brglmFit",
               data = glm_df)

brglm5_r <- brglm5 %>%
  coeftest(vcovHC(brglm5, type = 'HC0', cluster = c('iso3c', 'Summary')))



brglm6 <- glm(doc ~ approval + opp1vote + number_protests + previous_doc + duck + partyage + polity2 + left_exec + 
               any_election + minister_is_technocrat_mainstream + 
               resource_rents_lag + discovery + real_oil_price_log + gdp_pc_constant_lag + gdp_growth_lag + imf_program + crisis +
               as.factor(iso3c) + as.factor(quarter) + as.factor(year), 
               family = binomial(link="logit"), 
               method = "brglmFit",
               data = glm_df)

brglm6_r <- brglm6 %>%
  coeftest(vcovHC(brglm6, type = 'HC0', cluster = c('iso3c', 'Summary')))


# generate table
stargazer(brglm1_r,brglm2_r,brglm3_r,brglm4_r,brglm5_r,brglm6_r, #type="text",
          omit=c("iso3c","quarter","year"), no.space = T,
          covariate.labels = c("Executive approval (perc)","Opposition vote share (perc)","Number of protests",
                               "Previous document = 1","Lame duck = 1", "Party age",
                               "Democracy","Left executive = 1","Election quarter = 1",
                               "Mainstream minister = 1","Resource rents (perc)","Field discovery = 1","Oil price (USD, log)",
                               "GDP per capita (1,000 USD)","GDP growth (perc)","IMF program = 1", "Crisis = 1"), 
          dep.var.labels = "Any document")

# retrieve n, aic, ll, etc
stargazer(brglm1,brglm2,brglm3,brglm4,brglm5,brglm6, #type="text",
          omit=c("iso3c","quarter","year"), no.space = T,
          dep.var.labels = "Any document")
